package com.hmdp.ThreadUtil;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 *@Author:fengxiaohui
 *@Description: 创建线程方法1
 *@Date:Create in 10:08 上午 2023/6/4
 *@Modified By:
 */
public class Thread01 {

    public static void main(String[] args) {
        //方式一：继承Thread类，使用内部匿名类方式
        Thread t1 = new Thread(){
            @Override
            public void run() {
                //执行业务代码
                System.out.println("Hello ThreadId:" + Thread.currentThread().getId() + " ThreadName: " + Thread.currentThread().getName());
            }
        };
        //执行线程
        t1.start();


    }
}

class Thread02{
    public static void main(String[] args) {
        //方式二：实现Runnable接口 配合Thread类
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //执行业务代码
                System.out.println("Hello ThreadId:" + Thread.currentThread().getId() + " ThreadName: " + Thread.currentThread().getName());

            }
        };
        //创建线程
        Thread t2 = new Thread(runnable);
        t2.start();
    }
}

class ThreadLambda{

    public static void main(String[] args) {
        //方式二：Lambda优化 实现Runnable接口 配合Thread类
        //情况一：使用@FunctionalInterface注解修饰的接口类并且抽象方法只有一个的情况下
        Runnable r = () -> {
            System.out.println("Hello ThreadId:" + Thread.currentThread().getId() + " ThreadName: " + Thread.currentThread().getName());
        };
        Thread t22 = new Thread(r,"t2");
        t22.start();

    }
}
/**
* @Description: FutureTask 配合 Thread
 * FutureTask<V> implements RunnableFuture<V>
 *     interface RunnableFuture<V> extends Runnable, Future<V>
 * FutureTask的构造方法(Callable<V> callable)
 * interface Callable<V> {
 *     /**
 *      * Computes a result, or throws an exception if unable to do so.
 *      *
 *      * @return computed result
 *      * @throws Exception if unable to compute a result
 *
 */
@Slf4j
class Thread03{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                //执行业务代码
                System.out.println("Hello ThreadId:" + Thread.currentThread().getId() + " ThreadName: " + Thread.currentThread().getName());
                Thread.sleep(3000);
                return 1;
            }
        });
        Thread t3 = new Thread(task,"t3");
        t3.start();

       log.debug("{} :" + task.get());
    }
}

class ThreadDemo{

    public static void main(String[] args) {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 12; i++) {
                    try {
                        Thread.sleep(2000);
                        System.out.println("Thread: " + Thread.currentThread().getName()+" running");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        };
        Thread t2 = new Thread(){
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    Thread.sleep(1500);
                    System.out.println("Thread: " + Thread.currentThread().getName()+" running");
                }

            }
        };
        t1.setName("t1");
        t2.setName("t2");
        System.out.println(t1.getState());
        t1.start();
        System.out.println(t1.getState());
        t2.start();



    }
}
